package com.looso.sfolder.net;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.looso.sfolder.R;
import com.looso.sfolder.data.FileData;
import com.looso.sfolder.data.FileListData;
import com.looso.sfolder.data.FileRequestData;
import com.looso.sfolder.data.UserInfo;
import com.looso.sfolder.security.DES;
import com.looso.sfolder.security.DeviceUtils;
import com.looso.sfolder.security.MD5;
import com.looso.sfolder.ui.MyApplication;

public class HttpsInterface {
	
	/**
	 * Login
	 */
	public static boolean login(String username ,String password){
		if(username==null || username.equals("") || password==null || password.equals("")){
			return false;
		}
		try {
			//1. generate request
			JSONObject requestJson = new JSONObject();
			requestJson.put("method", "login");
			requestJson.put("username", username);
			requestJson.put("password", MD5.toMd5(password.getBytes()));
			
			String imsi = DeviceUtils.getIMSI();			
			requestJson.put("imsi", imsi);
			
			//2.post & get response
			String url = UserInfo.getInstance().getHost() +"/" + MyApplication.getInstance().getString(R.string.REQUEST_URL);
			byte[] response = HttpConnectionDemo.sendHttpsPost(url, requestJson.toString().getBytes());
			if(response == null){
				return false;
			}
			
			//3. parse json to data
			JSONObject responseJson = new JSONObject(new String(response));
			boolean result = JsonTool.jsonObjectGetBoolean(responseJson, "result");
			if(result){
				String sessionid = JsonTool.jsonObjectGetString(responseJson, "info");
				if(sessionid!=null && !sessionid.equals("")){
					UserInfo.getInstance().setSessionId(sessionid);
				}else{
					return false;
				}
			}else{
				return false;
			}
			
			
		} catch (JSONException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * Get filelist
	 * @return
	 */
	public static FileListData getFileList(){		
		try {
			//1. generate request
			JSONObject requestJson = new JSONObject();
			requestJson.put("method", "getFileList");
			String session = UserInfo.getInstance().getSessionId();
			if(session==null || session.equals("")){
				return null;
			}
			requestJson.put("session", session);
			
			//2.post & get response
			String url = UserInfo.getInstance().getHost()+"/" + MyApplication.getInstance().getString(R.string.REQUEST_URL);
			byte[] response = HttpConnectionDemo.sendHttpsPost(url, requestJson.toString().getBytes());
			
			if(response == null){
				return null;
			}
				
			//3. parse json to data
			JSONObject responseJson = new JSONObject(new String(response));
			int count = JsonTool.jsonObjectGetInt(responseJson, "count");
			JSONArray array = JsonTool.jsonObjectGetJSONArray(responseJson, "doc");
			ArrayList<FileRequestData> datas = new ArrayList<FileRequestData>();
			for(int i=0;i<count;i++){
				FileRequestData d = new FileRequestData();					
				d.setName(JsonTool.jsonObjectGetString(array.getJSONObject(i),"name"));
				d.setId(JsonTool.jsonObjectGetString(array.getJSONObject(i),"id"));
				datas.add(d);
			}
			
			return new FileListData(count, datas);
			
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Get file infomation by url
	 * @param url
	 * @return
	 */
	public static FileData getFile(String id){
		try {
			//1. generate request
			JSONObject requestJson = new JSONObject();
			requestJson.put("method", "getFile");
			String session = UserInfo.getInstance().getSessionId();
			if(session==null || session.equals("")){
				return null;
			}
			requestJson.put("session", session);
//			String key = DES.randomKeyString(20);
//			requestJson.put("password", key);
			
			FileData data = new FileData();
//			data.setKey(key);
			
			requestJson.put("id", id);
			
			//2.post & get response			
			String url = UserInfo.getInstance().getHost()+"/" + MyApplication.getInstance().getString(R.string.REQUEST_URL);
			byte[] response = HttpConnectionDemo.sendHttpsPost(url, requestJson.toString().getBytes());			
			if(response == null){
				return null;
			}
				
			//3. parse json to data
			JSONObject responseJson = new JSONObject(new String(response));
			data.setId(JsonTool.jsonObjectGetString(responseJson, "id"));
			data.setName(JsonTool.jsonObjectGetString(responseJson, "name"));
			data.setCount(JsonTool.jsonObjectGetInt(responseJson, "page"));
			data.setPath(JsonTool.jsonObjectGetString(responseJson, "path"));
			data.setKey(JsonTool.jsonObjectGetString(responseJson, "key"));
			
			return data;
			
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
}
